﻿using System;
using System.Windows;
using System.Windows.Input;

namespace Kugar.Core.SL4.Control
{
    public class MouseClickManager
    {
        public event MouseButtonEventHandler DoubleClick;

        private readonly StopWatch  stopWatch=new StopWatch();

        public static readonly DependencyProperty MouseClickManagerProperty = DependencyProperty.RegisterAttached(
                                                                                                                          "MouseClickManager",
                                                                                                                          typeof(MouseClickManager),
                                                                                                                          typeof(UIElement),null);
                                                                                                                        



        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="MouseClickManager"/> is clicked.
        /// </summary>
        /// <value><c>true</c> if clicked; otherwise, <c>false</c>.</value>
        private bool Clicked { get; set; }

        /// <summary>
        /// Gets or sets the control.
        /// </summary>
        /// <value>The control.</value>
        public FrameworkElement Control { get; set; }

        /// <summary>
        ///     双击超时时间
        /// </summary>
        /// <value>The timeout.</value>
        public int Timeout { get; set; }

        /// <summary>
        ///     双击的最小时间(用于处理一些比如修改名称等操作,需要将响应的时间与双击事件分开),默认为0
        /// </summary>
        public int MinTime { set; get; }

        public MouseClickManager(FrameworkElement control, int timeout):this(control,0,timeout)
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MouseClickManager"/> class.
        /// </summary>
        /// <param name="control">The control.</param>
        public MouseClickManager(FrameworkElement control,int minTime, int timeout)
        {
            control.SetValue(MouseClickManagerProperty, this);

            this.Clicked = false;
            this.Control = control;
            this.Timeout = timeout;
            this.MinTime = minTime;

            control.Unloaded += HandlerUnLoad;
        }

        /// <summary>
        /// Handles the click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        public void HandleClick(object sender, MouseButtonEventArgs e)
        {
            lock (this)
            {
                if (this.Clicked) //之前已经得到一个Click，再次得到视为DoubleClick
                {
                    this.Clicked = false;

                    var time=stopWatch.Stop();

                    if (time>MinTime && time <= Timeout)
                    {
                        OnDoubleClick(sender, e);
                    }

                }
                else //捕获Click事件
                {
                    stopWatch.Reset();
                    stopWatch.Start();
                    this.Clicked = true;
                    //创建线程来对标志位进行置位
                    //ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
                    //Thread thread = new Thread(threadStart);
                    //thread.Start(e);
                }
            }
        }

        private void HandlerUnLoad(object  sender,EventArgs e)
        {
            this.Control.Unloaded -= HandlerUnLoad;
            this.Control.MouseLeftButtonDown -= HandleClick;
            this.Control.MouseLeftButtonUp -= HandleClick;
            this.Control.ClearValue(MouseClickManagerProperty);
            this.Control = null;
            this.stopWatch.Reset();
            //this.stopWatch.Stop();
        }

        ///// <summary>
        ///// Resets the thread.
        ///// </summary>
        ///// <param name="state">The state.</param>
        //private void ResetThread(object state)
        //{
        //    //就是说超过在第一个Click之后超过间隔时间没有收到第二个Click则认为是Click
        //    Thread.Sleep(this.Timeout);

        //    lock (this)
        //    {
        //        if (this.Clicked)
        //        {
        //            this.Clicked = false;
        //            OnClick(this, (MouseButtonEventArgs)state);
        //        }
        //    }
        //}


        /// <summary>
        /// Called when [double click].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MouseButtonEventHandler handler = DoubleClick;
            
            if (handler != null)
                handler(sender, e);
        }
    }



    public static class FrameworkElementClickManage
    {
        public static MouseClickManager LeftDoubleClickManage(this FrameworkElement element,int clickTimeout)
        {
            if (element==null)
            {
                return null;
            }

            var mcm = new MouseClickManager(element, clickTimeout);

            element.MouseLeftButtonUp += mcm.HandleClick;

            return mcm;
        }
    }
}
